home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / mappinge.jav < prev    next >
Encoding:
Text File  |  1995-10-22  |  2.7 KB  |  107 lines

  1. /*
  2.   File: MappingEnumeration.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   22Oct95  dl@cs.oswego.edu   Created.
  11.  
  12. */
  13.   
  14. package collections;
  15.  
  16. import java.util.Enumeration;
  17. import java.util.NoSuchElementException;
  18.  
  19. /**
  20.  *
  21.  * MappingEnumerations allow you to transform elements from
  22.  * other enumerations before they are seen by their `consumers'
  23.  * (i.e., the callers of `nextElement').
  24.  * <P>
  25.  * MappingEnumerations work as wrappers around other Enumerations.
  26.  * To build one, you need an existing Enumeration (perhaps one
  27.  * from coll.elements(), for some Collection coll), and a Function
  28.  * object (i.e., implementing interface Function). 
  29.  * For example, if you want to process only the parent() fields
  30.  * of java.awt.Component elements held by a collection coll
  31.  * you could write something of the form:
  32.  * <PRE>
  33.  * Enumeration e = coll.elements();
  34.  * Enumeration parents = MappingEnumeration(e, ParentFunction);
  35.  * while (parents.hasMoreElements()) 
  36.  *  doSomethingWith((Container)(parents.nextElement()));
  37.  * </PRE>
  38.  * To use this, you will also need to write a little class of the form:
  39.  * <PRE>
  40.  * class ParentFunction implements Function {
  41.  *  Object function(Object v) {
  42.  *    if (v instanceOf Component) return ((Component)v).getParent();
  43.  *    else return null;
  44.  *  }
  45.  * }
  46.  * </PRE>
  47.  * <P>
  48.  * (This requires too much set-up to be reasonable in most
  49.  * situations, but is invaluable in others.)
  50.  * @see collections.Function#function
  51.  * @author Doug Lea
  52.  * @version 0.93
  53.  *
  54.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  55.  *
  56. **/
  57.  
  58. public class MappingEnumeration implements Enumeration {
  59.  
  60. // instance variables
  61.  
  62. /**
  63.  * The enumeration we are wrapping
  64. **/
  65.  
  66.   private Enumeration src_;
  67.  
  68. /**
  69.  * The transformation function
  70. **/
  71.  
  72.   private Function fn_;
  73.  
  74. /**
  75.  * Make an enumeration wrapping src, returning p.function for each nextElement
  76. **/
  77.  
  78.   public MappingEnumeration(Enumeration src, Function p) {
  79.     src_ = src;
  80.     fn_ = p;
  81.   }
  82.  
  83. /**
  84.  * Implements java.util.Enumeration.hasMoreElements
  85. **/
  86.  
  87.   public synchronized boolean hasMoreElements() { 
  88.     return src_.hasMoreElements(); 
  89.   }
  90.  
  91. /**
  92.  * Implements java.util.Enumeration.nextElement.
  93. **/
  94.   public synchronized Object nextElement() {
  95.     if (!hasMoreElements())
  96.       throw new NoSuchElementException("exhausted enumeration");
  97.     else {
  98.       Object v = src_.nextElement();
  99.       Object result = fn_.function(v);
  100.       return result;
  101.     }
  102.   }
  103.  
  104. }
  105.   
  106.  
  107.